<?php

/*
	Class: FriendsFriends

	About: Author
		Jaybill McCarthy

	About: License
		<http://communit.as/docs/license>
*/
class FriendsFriends extends Friends_Db_Table {

	/* Group: Instance Variables */

	/*
		Variable: $_name
	*/
	protected $_name = 'friends_friends';

	/*
		Variable: $_primary
	*/
	protected $_primary = array('username','friend_username');

	/* Group: Instance Methods */

	/*
		Function: insert
	*/
	public function insert(array $data) {
		$timestamp = date("Y-m-d H:i:s") ;
		$data['invited_on'] = $timestamp;

   		// make friend
		$retval = parent::insert($data);

	   	//make recipricol friend
	   	$rec_friend = $data;
		$rec_friend['username'] 		= $data['friend_username'];
		$rec_friend['friend_username'] 	= $data['username'];
		$rec_friend['is_pending'] 		= 0;
		parent::insert($rec_friend);

		return $retval;
	}

	/*
		Function: update
	*/
	public function update(array $data, $where) {
		// get existing friend record
		$existing_friend = $this->fetchrow($where);

		if ($data['is_approved'] != $existing_friend->is_approved) {
			// we are approving a friend request, approve the recipricol friend
			$rec_friend_where = $this->getAdapter()->quoteInto('username = ?', $existing_friend->friend_username);
			$rec_friend_where .= $this->getAdapter()->QuoteInto(' and friend_username = ?', $existing_friend->username);
			parent::update(array('is_pending' => 0, 'is_approved' => 1), $rec_friend_where);
			$data['is_pending'] = 0;
		}
		return parent::update($data, $where);
	}

	/*
		Function: delete
	*/
	public function delete($where) {
		$friend = $this->fetchRow($where);
		//delete recipricol friend
		if (!is_null($friend)) {
			$rec_friend_where = $this->getAdapter()->quoteInto('username = ?', $friend->friend_username);
			$rec_friend_where .= $this->getAdapter()->QuoteInto(' and friend_username = ?', $friend->username);
			parent::delete($rec_friend_where);
		}
		return parent::delete($where);
	}

	/*
		Function: getRequestsByUsername
	*/
	public function getRequestsByUsername($username) {
		$where = $this->getAdapter()->quoteInto('is_approved = 0 and is_pending = 0 and username = ?', $username);
		return ($this->getCountByWhereClause($where));
	} 

	/*
		Function: getPendingByUsername
	*/
	public function getPendingByUsername($username) {
		$where = $this->getAdapter()->quoteInto('is_approved = 0 and is_pending = 1 and username = ?', $username);
		return ($this->getCountByWhereClause($where));
	}
	
	/*
		Function: getTotalByUsername
	*/
	public function getTotalByUsername($username) {
		$where = $this->getAdapter()->quoteInto('is_approved = 1 and username = ?', $username);
		return ($this->getCountByWhereClause($where));
	}	

	/*
		Function: isRequestPending
	*/
	public function isRequestPending($username, $friend_username) {
		$is_pending = false;
		$where = $this->getAdapter()->QuoteInto('username = ?', $username);
		$where .= $this->getAdapter()->QuoteInto(' and friend_username = ?', $friend_username);
		$friend_request = $this->fetchRow($where);
		if (!is_null($friend_request)) {
			if((boolean)$friend_request->is_pending) {
				$is_pending = true;
			}
		}
		return $is_pending;
	}

	/*
		Function: isFriend
	*/
	public function isFriend($username, $friend_username) {
		$is_friend = false;
		$where = $this->getAdapter()->quoteInto('username = ?', $username);
		$where .= $this->getAdapter()->quoteInto( ' and friend_username = ?', $friend_username);
		$friend_request = $this->fetchRow($where);
		if (!is_null($friend_request)) {
			if ((boolean)$friend_request->is_approved) {
				$is_friend = true;
			}
		}
		return $is_friend;
	}

}
